package com.mldong.modules.wf.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import com.mldong.exception.ServiceException;
import com.mldong.modules.wf.api.WfFlowEngineApi;
import com.mldong.modules.wf.entity.ProcessDefine;
import com.mldong.modules.wf.entity.ProcessInstance;
import com.mldong.modules.wf.entity.ProcessTask;
import com.mldong.modules.wf.enums.FlowConst;
import com.mldong.modules.wf.service.ProcessDefineService;
import com.mldong.modules.wf.service.ProcessInstanceService;
import com.mldong.modules.wf.service.ProcessTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 工作流引擎api实现类
 * @author mldong
 * @date 2025/03/09
 */
@Component
@RequiredArgsConstructor
public class WfFlowEngineProvider implements WfFlowEngineApi {
    private final ProcessDefineService processDefineService;
    private final ProcessInstanceService processInstanceService;
    private final ProcessTaskService processTaskService;
    @Override
    public Dict startAndExecute(Long processDefineId, Dict args) {
        return processInstanceService.startAndExecute(processDefineId, args);
    }

    @Override
    public Dict startAndExecute(String processDefineName, Dict args) {
        ProcessDefine processDefine = processDefineService.getLastByName(processDefineName);
        if(processDefine == null){
            ServiceException.throwBiz(99999999,"流程定义不存在！");
        }
        return processInstanceService.startAndExecute(processDefine.getId(), args);
    }

    @Override
    public Dict getProcessDefine(Long processDefineId) {
        ProcessDefine processDefine = processDefineService.getById(processDefineId);
        if(processDefine !=null){
            return BeanUtil.toBean(processDefine,Dict.class);
        }
        return null;
    }

    @Override
    public Dict getProcessDefine(String processDefineName) {
        ProcessDefine processDefine = processDefineService.getLastByName(processDefineName);
        if(processDefine !=null){
            return BeanUtil.toBean(processDefine,Dict.class);
        }
        return null;
    }

    @Override
    public Dict getProcessDefine(String processDefineName, Integer version) {
        ProcessDefine processDefine = processDefineService.getProcessDefineByVersion(processDefineName,version);
        if(processDefine !=null){
            return BeanUtil.toBean(processDefine,Dict.class);
        }
        return null;
    }

    @Override
    public Dict getProcessInstance(Long processInstanceId) {
        ProcessInstance processInstance = processInstanceService.getById(processInstanceId);
        if(processInstance !=null){
            return BeanUtil.toBean(processInstance,Dict.class);
        }
        return null;
    }

    @Override
    public Dict getProcessTask(Long processTaskId) {
        ProcessTask processTask = processTaskService.getById(processTaskId);
        if(processTask !=null){
            Dict res = BeanUtil.toBean(processTask,Dict.class);
            // 追加参与人信息
            res.put(FlowConst.ACTOR_IDS_KEY,processTaskService.getTaskActors(processTaskId));
            return res;
        }
        return null;
    }
}
